package com.example.auth.config;

import com.example.auth.service.ExampleMobileUserDetailService;
import com.example.auth.service.ExampleUserDetailsService;
import com.example.common.plugins.authentication.MobilePasswordAuthenticationProvider;
import com.example.common.plugins.authentication.WeixinLoginAuthenticationProvider;
import com.example.system.api.WeixinApi;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author 王令
 * @since 2022/7/29 17:06
 */
@RequiredArgsConstructor
@EnableWebSecurity(debug = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    private final ExampleUserDetailsService userDetailsService;
    private final ExampleMobileUserDetailService mobileUserDetailService;
    private final PasswordEncoder passwordEncoder;
    private final WeixinApi weixinApi;

    @SneakyThrows
    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(daoAuthenticationProvider())
            .authenticationProvider(mobilePasswordAuthenticationProvider())
            .authenticationProvider(weixinLoginAuthenticationProvider());
    }


    @SneakyThrows
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }

    @Primary
    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setHideUserNotFoundExceptions(false);
        provider.setUserDetailsService(userDetailsService);
        provider.setPasswordEncoder(passwordEncoder);
        return provider;
    }

    @Bean
    public MobilePasswordAuthenticationProvider mobilePasswordAuthenticationProvider() {
        final MobilePasswordAuthenticationProvider provider = new MobilePasswordAuthenticationProvider();
        provider.setPasswordEncoder(passwordEncoder);
        provider.setMobileUserDetailService(mobileUserDetailService);
        return provider;
    }

    @Bean
    public WeixinLoginAuthenticationProvider weixinLoginAuthenticationProvider() {
        final WeixinLoginAuthenticationProvider provider = new WeixinLoginAuthenticationProvider();
        provider.setWeixinApi(weixinApi);
        provider.setMobileUserDetailService(mobileUserDetailService);
        return provider;
    }
}
